<template>
  <div class="dashboard-container">
    <div class="dashboard-text" style="font-weight:20">欢迎回来! &nbsp;{{ name }} <span v-for="role in roles" :key="role">{{ role }}</span></div>
    <div class="dashboard-text" align="center">首页数据中心</div>
    <div class="chart-container">
      <br />
      <div id="chart" class="chart" style="height: 500px; width: 100%" />
    </div>
  </div>
      
</template>

<script>
import { mapGetters } from 'vuex'
import * as echarts from "echarts";
import sta from "@/api/sta";
export default {
  name: 'Dashboard',
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  data() {
    return {
      searchObj: {
        type: "all",
        begin: "",
        end: "",
      },
      chart: null,
      title: "",
      xAxis: [],
      legend: [],
      series: [],
      yAxis: [],
    };
  },
  created(){
    this.showChart()
  },
  methods: {
    showChart() {
      this.$message({
        type: "info",
        message: "正在统计生成本月数据",
      });

      this.initChartData();
    },

    // 准备图表数据
    initChartData() {
      sta.getChartData(this.searchObj).then((response) => {
        console.log(response.data)
        // console
        this.xAxis = response.data.dataMap.xAxis;
        this.legend = response.data.dataMap.legend;
        this.series = response.data.dataMap.series;
        this.yAxis = response.data.dataMap.yAxis;
        this.setChart();
        this.$message({
          type: "success",
          message: "统计生成成功",
        });
      });
    },

    // 设置图标参数
    setChart() {
      // 基于准备好的dom，初始化echarts实例
      this.chart = echarts.init(document.getElementById("chart"));
      // console.log(this.chart)

      // 指定图表的配置项和数据
      var option = {
        dataZoom: [{
  show: true,
  height: 25,
  xAxisIndex: [
    0
  ],
  bottom: 20,
  start: 10,
  end: 80,
  handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
  handleSize: '110%',
  handleStyle: {
    color: '#d3dee5'

  },
  textStyle: {
    color: '#fff'
  },
  borderColor: '#90979c'
},
{
  type: 'inside',
  show: true,
  height: 15,
  start: 1,
  end: 35
}],
        title: {
          text: "数据统计表",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ["line", "bar"] },
            restore: { show: true },
            saveAsImage: { show: true },
          },
        },
        legend: {
          data: this.legend,
        },
        xAxis: this.xAxis,
        yAxis: this.yAxis,
        series: this.series,
      };
      
      this.chart.setOption(option, true);
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
